#include "Rectemper_server.h"

static int callback(void *data, int argc, char **argv, char **azColName)
{
    int i;
    for(i=0; i<argc; i++)
    {
        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    printf("\n");
    return 0;
}

int Opendb_create_table(char *DB_NAME, sqlite3  **db,char *table_name)
{
    char    sql[1024] ;
    int     rv = -1 ;
    char    *zErrMsg = NULL ;
                        
    rv = sqlite3_open(DB_NAME, db) ; //Open RPI_temper.db
     if(rv != SQLITE_OK)
     {
        printf("%s open failure: %s", DB_NAME, sqlite3_errmsg(*db)) ;
        return ERROR;
    }
    printf("Open %s sucessful!\n", DB_NAME) ;

    /*  Create table    */
    snprintf(sql, sizeof(sql),"CREATE TABLE %s( "                                 \
                        "ID             INTEGER     PRIMARY KEY     AUTOINCREMENT," \
                        "SN             CHAR(64)    NOT NULL,"                      \
                        "TIME           CHAR(128)   NOT NULL,"                      \
                        "TEMPER         CHAR(32)    NOT NULL);", table_name) ;

    rv = sqlite3_exec(*db, sql, callback, 0, &zErrMsg) ;
    if(rv != SQLITE_OK)
    {
        printf("SQL error: %s\n", zErrMsg) ;
        sqlite3_free(zErrMsg) ;
    }
    return OK ;    
}
